package com.jackrain.nea.wms.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.wms.api.BaseWmsSaveSuplierCmd;
import com.jackrain.nea.wms.mapper.WmsSqlMapper;
import com.jackrain.nea.wms.utils.WmsQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;


@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version="1.0", group = "wms")
public class BaseWmsSaveSuplierCmdImpl extends CommandAdapter implements BaseWmsSaveSuplierCmd {

    @Autowired
    private WmsQueryService wmsQueryService;

    @Autowired
    private WmsSqlMapper wmsSqlMapper;

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        if (null == param || param.size() == 0 || param.getJSONArray("data").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }
        String mTable = param.getString("tablename");
        JSONArray dataArray = param.getJSONArray("data");
        log.debug("BaseWmsSaveSuplierCmdImpl.dataArray.size()-->{}",dataArray.size());
        for(int i=0;i<dataArray.size();i++){
            JSONObject mainObj = dataArray.getJSONObject(i);
            log.debug("BaseWmsSaveSuplierCmdImpl--mainObj-->{}",mainObj);
            JSONObject mainData = new JSONObject();
            //获取参数
            String is_default = mainObj.getString("ISDEFAULT");

            mainData.put("pro_code",mainObj.getString("PRO_CODE"));
            mainData.put("supplier_code",mainObj.getString("SUPPLIER_CODE"));
            mainData.put("is_default",is_default);

            //保存插入
            HashMap cTableMap = new HashMap();
            cTableMap.put("tableName", mTable);
            cTableMap.put("insertKeys", mainData);
            log.debug("BaseWmsSaveSuplierCmdImpl--cTableMap-->{}",cTableMap);
            wmsQueryService.commandInsert(cTableMap);
        }

        vh.put("code", 0);
        vh.put("message", Resources.getMessage("插入数据成功"));
        return vh;
    }
}
